<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap.css">
    <script src="./lib/jquery.js"></script>
</head>

<body>

    <div class="col-xs-10 col-sm-10 col-md-10 col-lg-10 col-md-offset-1">

        <!-- 评论面板 -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">留言板</h3>
            </div>

            <form action="" method="POST" role="form" id="addCmt">

                <div class="panel-body">
                    <div>留言人：</div>
                    <input type="text" name="username" id="input" class="form-control">
                    <div>留言内容：</div>
                    <textarea name="content" class="form-control"></textarea>
                    <button type="submit" class="btn btn-primary">发表留言</button>
                </div>
            </form>

        </div>

        <!-- 评论列表 -->
        <ul class="list-group">
            <!--     <li class="list-group-item">
                <span class="badge" style="background-color: #f0ad4e;">评论时间：</span><span class="badge" style="background-color: #5bc0de;">评论人：</span> Item 1
            </li> -->

        </ul>
    </div>
    <script>
        $(function() {
            getCommentList();
            // 刷新渲染列表
            function getCommentList() {
                $.ajax({
                    url: 'http://www.liulongbin.top:3006/api/cmtlist',
                    method: "get",
                    success: function(res) {
                        if (res.status !== 200) {
                            return alert('获取评论列表失败')
                        }
                        // console.log(res);
                        //渲染列表内容到页面上
                        let rows = [];
                        $.each(res.data, function(i, ele) {
                            rows.push(`<li class="list-group-item"><span class="badge" style="background-color: #f0ad4e;">评论时间：${ele.time}</span><span class="badge" style="background-color: #5bc0de;">评论人：${ele.username}</span>${ele.content}</li>`);
                        });
                        $(".list-group").empty().append(rows)
                    },
                })
            }
            // 发表评论
            $("#addCmt").on('submit', function(e) {
                e.preventDefault();
                let data = $(this).serialize();
                console.log(data);
                $.post('http://www.liulongbin.top:3006/api/addcmt', data, function(res) {
                    if (res.status !== 201) {
                        return alert('评论失败');
                    }
                    getCommentList();
                    // 清空表单信息 简便方法 用原生的DOM对象的reset()函数
                    $("#addCmt")[0].reset();
                })
            });

            //周期性的获取评论列表
            setInterval(function() {
                getCommentList();
            }, 1000);



        })
    </script>
</body>

</html>